package com.finalist.util;

import java.util.HashMap;
import java.util.Map;

public class PhoneLevelAndFeatureUtils
{
    /**
     * 获取手机号码等级及对应的规则
     * 等级范围（0-10）
     * @param phoneNum 手机号码
     * @return map key:level-等级 feature-规则特征值
     */
    public static Map<String, String> getLevelFeatureOfPhone(String phoneNum)
    {
        Map<String, String> resultMap = new HashMap<String, String>();
        String returnStr = "0";
        if (isLevel10(phoneNum, resultMap))
        {

            returnStr = "10";

        }
        else if (isLevel9(phoneNum, resultMap))
        {
            returnStr = "9";
        }
        else if (isLevel8(phoneNum, resultMap))
        {
            returnStr = "8";
        }
        else if (isLevel7(phoneNum, resultMap))
        {
            returnStr = "7";
        }
        else if (isLevel6(phoneNum, resultMap))
        {
            returnStr = "6";
        }
        else if (isLevel5(phoneNum, resultMap))
        {
            returnStr = "5";
        }
        else if (isLevel4(phoneNum, resultMap))
        {
            returnStr = "4";
        }
        else if (isLevel3(phoneNum, resultMap))
        {
            returnStr = "3";
        }
        else if (isLevel2(phoneNum, resultMap))
        {
            returnStr = "2";
        }
        else if (isLevel1(phoneNum, resultMap))
        {
            returnStr = "1";
        }
        else if (isLevel0(phoneNum, resultMap))
        {
            returnStr = "0";
        }
        resultMap.put("level", returnStr);
        return resultMap;
    }

    /**
     * 号码等级:10
     * 号码特征标准:*AAAAAA 、*00000 、*66666 、
     *              *88888、*99999、*XXXXYYYY
     * 靓号规则说明：A 不等于 4；X、Y 为任意数
     * @return
     */

    public static boolean isLevel10(String phoneNum, Map<String, String> resultMap)
    {
        String last8Str = getLastSubString(phoneNum, 8);//后8位字符串
        String last6Str = getLastSubString(phoneNum, 6);//后6位字符串
        String last5Str = getLastSubString(phoneNum, 5);//后5位字符串
        String rule3 = "([0-9])\\1{3}([0-9])\\2{3}";//*XXXXYYYY
        if (last6Str.matches("([0-9&&[^4]])\\1{5}"))
        {//*AAAAAA,连续相同的6位数字,除去4;
            resultMap.put("feature", "*AAAAAA");
            return true;
        }
        else if ("00000".equals(last5Str))
        {
            resultMap.put("feature", "*00000");
            return true;
        }
        else if ("66666".equals(last5Str))
        {
            resultMap.put("feature", "*66666");
            return true;
        }
        else if ("88888".equals(last5Str))
        {
            resultMap.put("feature", "*88888");
            return true;
        }
        else if ("99999".equals(last5Str))
        {
            resultMap.put("feature", "*99999");
            return true;
        }
        else if (last8Str.matches(rule3))
        {
            resultMap.put("feature", "*XXXXYYYY");
            return true;
        }
        return false;
    }

    /**
     * 号码等级:9
     * 号码特征标准:*AAAAA、*0000、*6666、*8888、
     *             *9999、*444444
     * 靓号规则说明：A 不等于 4；X、Y 为任意数
     * @return
     */
    public static boolean isLevel9(String phoneNum, Map<String, String> resultMap)
    {
        String last6Str = getLastSubString(phoneNum, 6);//后6位字符串
        String last4Str = getLastSubString(phoneNum, 4);//后6位字符串
        String rule1 = "[0-9]{1}([0-9&&[^4]])\\1{4}";//*AAAAA，连续相同的5位数字,除去4;
        if (last6Str.matches(rule1))
        {//*AAAAA，连续相同的5位数字,除去4;
            resultMap.put("feature", "*AAAAA");
            return true;
        }
        else if ("0000".equals(last4Str))
        {
            resultMap.put("feature", "*0000");
            return true;
        }
        else if ("6666".equals(last4Str))
        {
            resultMap.put("feature", "*6666");
            return true;
        }
        else if ("8888".equals(last4Str))
        {
            resultMap.put("feature", "*8888");
            return true;
        }
        else if ("9999".equals(last4Str))
        {
            resultMap.put("feature", "*9999");
            return true;
        }
        else if ("444444".equals(last6Str))
        {
            resultMap.put("feature", "*444444");
            return true;
        }
        return false;
    }

    /**
     * 号码等级:8
     * 号码特征标准:*44444 、 *AAAA 、 *BCDEFGH 、
     *              *WXYZWXYZ 、 *XYXYXYXY 、
     *              *WWXXYYZZ
     * 靓号规则说明：A 不等于4*BCDEFGH为累加数或递减数；W、X、Y、Z 为任意数
     * @return
     */
    public static boolean isLevel8(String phoneNum, Map<String, String> resultMap)
    {

        String last8 = getLastSubString(phoneNum, 8);
        String last5 = getLastSubString(phoneNum, 5);
        String last4 = getLastSubString(phoneNum, 4);
        String last2 = getLastSubString(phoneNum, 2);
        if ("44444".equals(last5))
        {//连续相同的5位数字,范围：*44444
            resultMap.put("feature", "*44444");
            return true;
        }
        else if (last4.matches("([0-9&&[^4]])\\1{3}"))
        {
            resultMap.put("feature", "*AAAA");
            return true;
        }
        else if (isUpStr(phoneNum, 7) || isDownStr(phoneNum, 7))
        {// *BCDEFGH ,*BCDEFGH为累加数或递减数
            resultMap.put("feature", "*BCDEFGH");
            return true;
        }
        else if (StringUtils.countSubStrWithOutCover(last8, last4) == 2)
        {//*WXYZWXYZ,W、X、Y、Z 为任意数
            resultMap.put("feature", "*WXYZWXYZ");
            return true;
        }
        else if (StringUtils.countSubStrWithOutCover(last8, last2) == 4)
        {//*XYXYXYXY ,W、X、Y、Z 为任意数
            resultMap.put("feature", "*XYXYXYXY");
            return true;
        }
        else if (last8.matches("([0-9])\\1{1}([0-9])\\2{1}([0-9])\\3{1}([0-9])\\4{1}"))
        {// *WWXXYYZZ,W、X、Y、Z 为任意数
            resultMap.put("feature", "*WWXXYYZZ");
            return true;
        }
        return false;
    }

    /**
     * 号码等级:7
     * 号码特征标准:*000、 *666、 *888、 *999、
     *              *4444 、 *ABCDEF 、 *XXYYZZ 、
     *              *XYXYXY 、*WW88 、*88WWW 、
     *              *W88W88、*XXXYYY
     * 靓号规则说明：*ABCDEF 为累加数或递减数；X、Y、Z 为任意数；W 不等于4
     * @return
     */
    public static boolean isLevel7(String phoneNum, Map<String, String> resultMap)
    {
        String last6 = getLastSubString(phoneNum, 6);
        String last4 = getLastSubString(phoneNum, 4);
        String last2 = getLastSubString(phoneNum, 2);
        String last3 = getLastSubString(phoneNum, 3);

        if ("000".equals(last3))
        {
            resultMap.put("feature", "*000");
            return true;
        }
        else if ("666".equals(last3))
        {
            resultMap.put("feature", "*666");
            return true;
        }
        else if ("888".equals(last3))
        {
            resultMap.put("feature", "*888");
            return true;
        }
        else if ("999".equals(last3))
        {
            resultMap.put("feature", "*999");
            return true;
        }
        else if ("4444".equals(last4))
        {
            resultMap.put("feature", "*4444");
            return true;
        }
        else if (isUpStr(phoneNum, 5) || isDownStr(phoneNum, 5))
        {//*ABCDEF，*ABCDEF 为累加数或递减数
            resultMap.put("feature", "*ABCDEF");
            return true;
        }
        else if (last6.matches("([0-9])\\1{1}([0-9])\\2{1}([0-9])\\3{1}"))
        {// *XXYYZZ,W、X、Y、Z 为任意数
            resultMap.put("feature", "*XXYYZZ");
            return true;
        }
        else if (StringUtils.countSubStrWithOutCover(last6, last2) == 3)
        {//*XYXYXY,X、Y、Z 为任意数；
            resultMap.put("feature", "*XXXYYY");
            return true;
        }
        else if (last4.matches("([0-9&&[^4]])\\1{1}([8]])\\2{1}"))
        {//*WW88，W 不等于4
            resultMap.put("feature", "*WW88");
            return true;
        }
        else if (last6.matches("[0-9]{1}[8]{2}([0-9&&[^4]])\\1{2}"))
        {//*88WWW，W 不等于4
            resultMap.put("feature", "*88WWW");
            return true;
        }
        else if (last6.matches("[0-9&&[^4]]{1}[8]{2}[0-9&&[^4]]{1}[8]{2}") && (last4.charAt(0) == last4.charAt(3)))
        {//*W88W88，W 不等于4
            resultMap.put("feature", "*W88W88");
            return true;
        }
        else if (last6.matches("([0-9]])\\1{2}([0-9]])\\2{2}"))
        {// *XXXYYY,X、Y、Z 为任意数
            resultMap.put("feature", "*XXXYYY");
            return true;
        }

        return false;
    }

    /**
     * 号码等级:6
     * 号码特征标准:*1588、 *1688、 *AAA、 *BCDEF、
     *              *A8A8、 *88AA、 *XYZXYZ
     * 靓号规则说明：A 不等 4；BCDEF 为累加数或递减数；X、Y、Z 为任意数；
     * @return
     */
    public static boolean isLevel6(String phoneNum, Map<String, String> resultMap)
    {
        String last6 = getLastSubString(phoneNum, 6);
        String last4 = getLastSubString(phoneNum, 4);

        if ("1588".equals(last4))
        {
            resultMap.put("feature", "*1588");
            return true;
        }
        else if ("1688".equals(last4))
        {
            resultMap.put("feature", "*1688");
            return true;
        }
        else if (last4.matches("[0-9]{1}([0-9&&[^4]])\\1{2}"))
        {// *AAA,A 不等 4
            resultMap.put("feature", "*AAA");
            return true;
        }
        else if (isUpStr(phoneNum, 5) || isDownStr(phoneNum, 5))
        {// *BCDEF，BCDEF 为累加数或递减数
            resultMap.put("feature", "*BCDEF");
            return true;
        }
        else if (last4.matches("[0-9&&[^4]]{1}[8]{1}[0-9&&[^4]]{1}[8]{1}") && (last4.charAt(0) == last4.charAt(2)))
        {// *A8A8,A 不等 4
            resultMap.put("feature", "*A8A8");
            return true;
        }
        else if (last4.matches("[8]{2}([0-9&&[^4]])\\1{1}"))
        {//*88AA,A 不等 4
            resultMap.put("feature", "*88AA");
            return true;
        }
        else if (StringUtils.countSubStrWithOutCover(last6, last6.substring(last6.length() - 3, last6.length())) == 2)
        {//*XYZXYZ
            resultMap.put("feature", "*XYZXYZ");
            return true;
        }

        return false;
    }

    /**
     * 号码等级:5
     * 号码特征标准:*5678、 *6789、 *0001、 *444、
     *              *AB88、 *AABB、 *888A、*AAA8、
     *              *AAAAAB
     * 靓号规则说明：A 不等于 B；A、B 均不等于 4
     * @return
     */
    public static boolean isLevel5(String phoneNum, Map<String, String> resultMap)
    {
        String last6 = getLastSubString(phoneNum, 6);
        String last4 = getLastSubString(phoneNum, 4);

        if ("5678".equals(last4))
        {//*5678
            resultMap.put("feature", "*5678");
            return true;
        }
        else if ("6789".equals(last4))
        {// *6789
            resultMap.put("feature", "*6789");
            return true;
        }
        else if ("0001".equals(last4))
        {//*0001
            resultMap.put("feature", "*0001");
            return true;
        }
        else if (last4.matches("[0-9]{1}([4])\\1{2}"))
        {//*444
            resultMap.put("feature", "*444");
            return true;
        }
        else if (last4.matches("[0-9&&[^4]]{2}[8]{2}") && (last4.charAt(0) != last4.charAt(1)))
        {//  *AB88,A、B 均不等于 4，A 不等于 B
            resultMap.put("feature", "*AB88");
            return true;
        }
        else if (last4.matches("([0-9[^4]])\\1{1}(0-9[^4])\\1{1}") && (last4.charAt(0) != last4.charAt(2)))
        { //*AABB,A、B 均不等于 4，A 不等于 B
            resultMap.put("feature", "*AABB");
            return true;
        }
        else if (last4.matches("[8]{3}[0-9&&[^4]]{1}"))
        {//*888A,A、B 均不等于 4
            resultMap.put("feature", "*888A");
            return true;
        }
        else if (last4.matches("([0-9&&[^4]])\\1{2}[8]{1}"))
        {//*AAA8,A、B 均不等于 4
            resultMap.put("feature", "*AAA8");
            return true;
        }
        else if (last6.matches("([0-9&&[^4]])\\1{4}[0-9&&[^4]]{1}") && (last6.charAt(4) != last6.charAt(5)))
        {//*AAAAAB,A、B 均不等于 4，A 不等于 B；
            resultMap.put("feature", "*AAAAAB");
            return true;
        }

        return false;
    }

    /**
     * 号码等级:4
     * 号码特征标准:*ABCD、*X168、*X158、*X518、
     *              *8YY8、*XXXXY
     * 靓号规则说明：ABCD 为累加数或递减数；X、Y 均不等于 4；X 不等于 Y
     * @return
     */
    public static boolean isLevel4(String phoneNum, Map<String, String> resultMap)
    {
        String last5 = getLastSubString(phoneNum, 5);
        String last4 = getLastSubString(phoneNum, 4);
        if (isUpStr(phoneNum, 4) || isDownStr(phoneNum, 4))
        {//*ABCD,ABCD 为累加数或递减数；
            resultMap.put("feature", "*ABCD");
            return true;
        }
        else if (last4.matches("[0-9&&[^4]]{1}168"))
        {//*X168,X、Y 均不等于 4
            resultMap.put("feature", "*X168");
            return true;
        }
        else if (last4.matches("[0-9&&[^4]]{1}158"))
        {//*X158,X、Y 均不等于 4
            resultMap.put("feature", "*X158");
            return true;
        }
        else if (last4.matches("[0-9&&[^4]]{1}518"))
        {//*X518,X、Y 均不等于 4
            resultMap.put("feature", "*X518");
            return true;
        }
        else if (last4.matches("[8]{1}([0-9&&[^4]])\\1{1}[8]{1}"))
        {//*8YY8,X、Y 均不等于 4
            resultMap.put("feature", "*8YY8");
            return true;
        }
        else if (last5.matches("([0-9&&[^4]])\\1{3}[0-9&&[^4]]{1}") && (last5.charAt(0) != last5.charAt(last5.length() - 1)))
        {//*XXXXY,X、Y 均不等于 4；X 不等于 Y
            resultMap.put("feature", "*XXXXY");
            return true;
        }
        return false;
    }

    /**
     * 号码等级:3
     * 号码特征标准:*ABBA、*AAAB、*AB00、*AB66、
     *              *AB99、*ABAB
     * 靓号规则说明：A、B 均不等于 4
     * @return
     */
    public static boolean isLevel3(String phoneNum, Map<String, String> resultMap)
    {
        String last4 = getLastSubString(phoneNum, 4);
        if (last4.matches("[0-9&&[^4]]{1}([0-9&&[^4]])\\1{1}[0-9&&[^4]]{1}") && (last4.charAt(0) == last4.charAt(3)))
        {//*ABBA，A、B 均不等于 4
            resultMap.put("feature", "*ABBA");
            return true;
        }
        else if (last4.matches("([0-9&&[^4]])\\1{2}[0-9&&[^4]]{1}"))//&& (last4.charAt(2) != last4.charAt(3))
        {//*AAAB，A、B 均不等于 4
            resultMap.put("feature", "*AAAB");
            return true;
        }
        else if (last4.matches("[0-9&&[^4]]{2}[0]{2}"))// && (last4.charAt(0) != last4.charAt(1))
        {//*AB00，A、B 均不等于 4
            resultMap.put("feature", "*AB00");
            return true;
        }
        else if (last4.matches("[0-9&&[^4]]{2}[6]{2}"))// && (last4.charAt(0) != last4.charAt(1))
        {//*AB66，A、B 均不等于 4
            resultMap.put("feature", "*AB66");
            return true;
        }
        else if (last4.matches("[0-9&&[^4]]{2}[9]{2}"))// && (last4.charAt(0) != last4.charAt(1))
        {//*AB99，A、B 均不等于 4
            resultMap.put("feature", "*AB99");
            return true;
        }
        else if (last4.split(getLastSubString(last4, 2)).length == 2)
        {//*ABAB，A、B 均不等于 4
            resultMap.put("feature", "*ABAB");
            return true;
        }

        return false;
    }

    /**
     * 号码等级:2
     * 号码特征标准:*ABC、*XX、*X88Y、*8
     * 靓号规则说明：ABC 为累加数或递减数；X 不等于 Y；X、Y均不等于 4
     * @return
     */
    public static boolean isLevel2(String phoneNum, Map<String, String> resultMap)
    {
        String last2 = phoneNum.substring(phoneNum.length() - 2, phoneNum.length());
        String last4 = phoneNum.substring(phoneNum.length() - 4, phoneNum.length());

        if (isUpStr(phoneNum, 3) || isDownStr(phoneNum, 3))
        {//*ABC，ABC 为累加数或递减数
            resultMap.put("feature", "*ABC");
            return true;
        }
        else if (last2.matches("([0-9&&[^4]])\\1{1}"))
        {//*XX，其包含*8,X 不等于 Y；X、Y均不等于 4
            resultMap.put("feature", "*XX");
            return true;
        }
        else if (last4.matches("[0-9&&[^4]]{1}[8]{2}[0-9&&[^4]]{1}") && last4.charAt(0) != last4.charAt(3))
        {//*X88Y,X 不等于 Y；X、Y均不等于 4
            resultMap.put("feature", "*X88Y");
            return true;
        }
        else if (last2.matches("[0-9]{1}8"))
        {//*8
            resultMap.put("feature", "*8");
            return true;
        }

        return false;
    }

    /**
     * 号码等级:1
     * 号码特征标准:普通号
     * 靓号规则说明：除0 级、2-10 级外，号码最后3 位均不等于4的号码
     * @return
     */
    public static boolean isLevel1(String phoneNum, Map<String, String> resultMap)
    {
        String rule = "[0-9&&[^4]]{3}";//除0 级、2-10 级外，号码最后3 位均不等于4的号码
        if (getLastSubString(phoneNum, 3).matches(rule))
        {
            resultMap.put("feature", "普通号");
            return true;
        }
        return false;
    }

    /**
     * 号码等级:0
     * 号码特征标准:特号（差号）
     * 靓号规则说明：除1-10 级外，号码最后三位中任意一位等于 4 的号码
     * @return
     */
    public static boolean isLevel0(String phoneNum, Map<String, String> resultMap)
    {
        String last3 = getLastSubString(phoneNum, 3);
        for (int i = 0; i < last3.length(); i++)
        {
            if (last3.charAt(i) == '4')
            {
                resultMap.put("feature", "特号(差号)");
                return true;
            }
        }
        return false;
    }

    /**
     * 截取某字符后几位字符串
     * @param orgStr 原字符串
     * @param length 截取的字符串长度
     * @return
     */
    public static String getLastSubString(String orgStr, int length)
    {

        return orgStr.substring(orgStr.length() - length, orgStr.length());
    }

    /**
     * 判断是否为递增的数字字符串（左到右）
     * @param str 数字字符串
     * @return
     */
    public static boolean isUpStr(String str, int length)
    {
        str = str.substring(str.length() - length, str.length());
        for (int i = 0; i < str.length(); i++)
        {
            if ((i + 1) != str.length())
            {
                if ((Integer.valueOf(str.charAt(i)) + 1) != Integer.valueOf(str.charAt(i + 1)))
                {
                    return false;
                }
            }
        }
        return true;
    }

    /**
     * 判断是否为递减的数字字符串（左到右）
     * @param str 数字字符串
     * @return
     */
    public static boolean isDownStr(String str, int length)
    {
        str = str.substring(str.length() - length, str.length());
        for (int i = 0; i < str.length(); i++)
        {
            if ((i - 1) != str.length())
            {
                if ((Integer.valueOf(str.charAt(i)) + 1) != Integer.valueOf(str.charAt(i + 1)))
                {
                    return false;
                }
            }
        }
        return true;

    }

    public static void main(String[] args)
    {
        //        Map<String, String> mapStr = new HashMap<String, String>();
        //        System.out.println(getLevelOfPhone("18212341234", mapStr));
        //        System.out.println(isLevel10("12345222222")); //true
        //        System.out.println(isLevel10("12345222622")); //false
        //        System.out.println(isLevel10("12345444444")); //false
        //        System.out.println(isLevel10("12345200000"));//true
        //        System.out.println(isLevel10("12345266666"));//true
        //        System.out.println(isLevel10("12345288888"));//true
        //        System.out.println(isLevel10("12345299999"));//true
        //        System.out.println(isLevel10("12344445555"));//true
        //        System.out.println(isLevel10("12366775555"));//false
        //        System.out.println(isLevel10("12355555555")); //true
        //        String rule5 = "([0-9])\\1{1}([0-9])\\2{1}([0-9])\\3{1}([0-9])\\4{1}";// *WWXXYYZZ,W、X、Y、Z 为任意数
        //        System.out.println("11223344".matches(rule5));
        //        String rule4 = "(?:([0-9])\\1{4})+";//*XYXYXYXY ,W、X、Y、Z 为任意数
        //        System.out.println("12341234".matches(rule4));
        //System.out.println(isUpStr("123456789", 4));

        //        String rule1 = "([0|6|8|9])\\1{2}";//*000、 *666、 *888、 *999
        //
        //        System.out.println("666".matches(rule1));

        //System.out.println("113".charAt(0) == "113".charAt(1));
        //        String rule4 = "([0-9&&[^4]])\\1{2}[0-9&&[^4]]{1}";//*888A、*AAA8,A、B 均不等于 4
        //        System.out.println("8888".matches(rule4));
        // String rule1 = "[0-9&&[^4]]{1}[1]{1}[6]{1}[8]{1}|[0-9&&[^4]]{1}[1]{1}[5]{1}[8]{1}|[0-9&&[^4]]{1}[5]{1}[1]{1}[8]{1}";//*X168、*X158、*X518、X、Y 均不等于 4
        //        String rule = "[0-9]{2}([0|6|8|9])\\1{3}";
        //        System.out.println("116666".matches(rule));
        //        String last8 = "XYXYXYXY";
        //
        //        boolean rule5 = last8.c("XY").length == 4 ? true : false;//*XYXYXYXY ,W、X、Y、Z 为任意数
        //        System.out.println(last8.split("XY").length);

        //        boolean rule4 = StringUtils.countSubStrWithOutCover("WXYZWXYZ", "WXYZ") == 2 ? true : false;//*WXYZWXYZ,W、X、Y、Z 为任意数
        //        boolean rule5 = StringUtils.countSubStrWithOutCover("XYXYXYXY", "XY") == 4 ? true : false;//*XYXYXYXY ,W、X、Y、Z 为任意数
        //        System.out.println(rule4);
        //        System.out.println(rule5);
        //        System.out.println("88".matches("88"));

        //Map<String, String> mapStr = new HashMap<String, String>();
        //        System.out.println(mapStr);

//        System.out.println(getLevelFeatureOfPhone("182011483333"));
        
        System.out.println(getLevelFeatureOfPhone("17092131688"));
        
        
        

    }
}
